<script setup lang="tsx">
import { reactive, ref, watch, onMounted, unref } from 'vue'
import { Form, FormSchema } from '@/components/Form'
import { ElCheckbox } from 'element-plus'
import { useForm } from '@/hooks/web/useForm'
import { useUserStore } from '@/store/modules/user'
import { useValidator } from '@/hooks/web/useValidator'
import { BaseButton } from '@/components/Button'
import { Icon } from '@/components/Icon'

const emit = defineEmits(['to-verification'])

const { required } = useValidator()
const userStore = useUserStore()

const rules = {
  // username: [required()],
  // password: [required()]
}

const schema = reactive<FormSchema[]>([
  {
    field: 'title',
    colProps: {
      span: 24
    },
    formItemProps: {
      slots: {
        default: () => {
          return (
            <> 
            <div class="logoImg flex justify-center w-[100%]">
              <img src='/src/assets/login/logo.png' />
              </div>
            </>
          )
        }
      },
    },
  },
  {
    field: 'username',
    component: 'Input',
    colProps: {
      span: 24
    },
    componentProps: {
      placeholder: '请输入系统编号',
      prefixIcon: <Icon icon="svg-icon:orgId" />
    },
  },
  {
    field: 'password',
    component: 'Input',
    colProps: {
      span: 24
    },
    componentProps: {
      placeholder: '请输入用户名/手机号',
      prefixIcon: <Icon icon="svg-icon:user" />
    },
  },
  {
    field: 'password12',
    component: 'InputPassword',
    colProps: {
      span: 24
    },
    componentProps: {
      placeholder: '输入登录密码',
      prefixIcon: <Icon icon="svg-icon:password" />,
      onKeydown: (_e: any) => {
        if (_e.key === 'Enter') {
          _e.stopPropagation()
          signIn()
        }
      }
    },
  },
  {
    field: 'tool',
    colProps: {
      span: 24
    },
    formItemProps: {
      slots: {
        default: () => {
          return (
            <>
              <div class="flex justify-between items-center w-[100%] remeber">
                <ElCheckbox v-model={remember.value} label="记住账号" size="small" />
              </div>
            </>
          )
        }
      },
    }
  },
  {
    field: 'login',
    colProps: {
      span: 24
    },
    formItemProps: {
      slots: {
        default: () => {
          return (
            <>
              <div class="loginBtn w-[100%]">
                <BaseButton
                  loading={loading.value}
                  type="primary"
                  class="w-[100%]"
                  onClick={signIn}
                >
                 登录
                </BaseButton>
              </div>
            </>
          )
        }
      }
    }
  }
])

const remember = ref(userStore.getRememberMe)
const loading = ref(false)

const initLoginInfo = () => {
  const loginInfo = userStore.getLoginInfo
  if (loginInfo) {
    const { username, password } = loginInfo
    setValues({ username, password })
  }
}

onMounted(() => {
  initLoginInfo()
})

const { formRegister, formMethods } = useForm()
const { getFormData, getElFormExpose, setValues } = formMethods

const signIn = async () => {
  const formRef = await getElFormExpose()
  await formRef?.validate(async (isValid) => {
    if (isValid) {
      loading.value = true
      const formData = await getFormData()
      
      try {
        // 模拟验证成功
        emit('to-verification', formData)
      } finally {
        loading.value = false
      }
    }
  })
}
</script>

<template>
  <Form
    :schema="schema"
    :rules="rules"
    label-position="top"
    hide-required-asterisk
    size="large"
    class="logon-form dark:(border-1 border-[var(--el-border-color)] border-solid)"
    @register="formRegister"
  />
</template>

<style lang="less" scoped>
/* 样式保持不变 */
</style>